{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8af51de0",
   "metadata": {},
   "source": [
    "# 3.4 函数\n",
    "## 概念：\n",
    "* 函数是一个能够完成特定功能的代码块，可在程序中重复使用，减少程序的代码量和提高程序的执行效率\n",
    "## 定义语法："
   ]
  },
  {
   "cell_type": "raw",
   "id": "ba12e3d4",
   "metadata": {},
   "source": [
    " def function_name([arg1],[arg2],…):\n",
    "        [expression]\n",
    "        return [value]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bc95633",
   "metadata": {},
   "source": [
    "### 缺省参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "949ed245",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: 3\n",
      "y: 2\n",
      "z: 1\n"
     ]
    }
   ],
   "source": [
    "def f(x = 3,y = 2, z = 1):\n",
    "    print(f\"x: {x}\")\n",
    "    print(f\"y: {y}\")\n",
    "    print(f\"z: {z}\")\n",
    "f()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9060e47e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x = 3, y = 2, z = 1, *args, **kwargs):\n",
    "    print(f\"x: {x}\")\n",
    "    print(f\"y: {y}\")\n",
    "    print(f\"z: {z}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8a1479d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: 1\n",
      "y: 2\n",
      "z: 3\n"
     ]
    }
   ],
   "source": [
    "f(1,2,3,4,5,6,7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ded09233",
   "metadata": {},
   "source": [
    "#### 通用求和函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c8e447de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def mysum(*args):\n",
    "    return sum(args)\n",
    "mysum(1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8c74d5c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
